package com.it.util;

import java.time.*;
import java.util.Date;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/28 9:35
 */
public class TimeUtils {
    private static LocalDate today = LocalDate.now();

    /*获取当前开始时间*/
    public static Date startDay() {
        LocalDateTime localDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDate local = localDateTime.toLocalDate();
        return Date.from(local.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取当前结束时间*/
    public static Date endDay() {
        LocalDateTime localDateTime = LocalDateTime.of(today.plusDays(1), LocalTime.MIN);
        LocalDate local = localDateTime.toLocalDate();
        return Date.from(local.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取本月开始时间*/
    public static Date startMonth(){
        LocalDate local = today.withDayOfMonth(1);
        return Date.from(local.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取本月结束时间*/
    public static Date endMonth(){
        LocalDate local = today.withDayOfMonth(LocalDate.MAX.lengthOfMonth());
        return Date.from(local.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取本季开始时间*/
    public static Date startSeason(){
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentYear = currentDate.getYear();
        LocalDate firstDay, lastDay;
        if (currentMonth == Month.JANUARY || currentMonth == Month.FEBRUARY || currentMonth == Month.MARCH) {
            firstDay = LocalDate.of(currentYear, Month.JANUARY, 1);
            lastDay = LocalDate.of(currentYear, Month.MARCH, 31);
        } else if (currentMonth == Month.APRIL || currentMonth == Month.MAY || currentMonth == Month.JUNE) {
            firstDay = LocalDate.of(currentYear, Month.APRIL, 1);
            lastDay = LocalDate.of(currentYear, Month.JUNE, 30);
        } else if (currentMonth == Month.JULY || currentMonth == Month.AUGUST || currentMonth == Month.SEPTEMBER) {
            firstDay = LocalDate.of(currentYear, Month.JULY, 1);
            lastDay = LocalDate.of(currentYear, Month.SEPTEMBER, 30);
        } else {
            firstDay = LocalDate.of(currentYear, Month.OCTOBER, 1);
            lastDay = LocalDate.of(currentYear, Month.DECEMBER, 31);
        }
        return Date.from(firstDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取本季结束时间*/
    public static Date endSeason(){
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentYear = currentDate.getYear();
        LocalDate firstDay, lastDay;
        if (currentMonth == Month.JANUARY || currentMonth == Month.FEBRUARY || currentMonth == Month.MARCH) {
            firstDay = LocalDate.of(currentYear, Month.JANUARY, 1);
            lastDay = LocalDate.of(currentYear, Month.MARCH, 31);
        } else if (currentMonth == Month.APRIL || currentMonth == Month.MAY || currentMonth == Month.JUNE) {
            firstDay = LocalDate.of(currentYear, Month.APRIL, 1);
            lastDay = LocalDate.of(currentYear, Month.JUNE, 30);
        } else if (currentMonth == Month.JULY || currentMonth == Month.AUGUST || currentMonth == Month.SEPTEMBER) {
            firstDay = LocalDate.of(currentYear, Month.JULY, 1);
            lastDay = LocalDate.of(currentYear, Month.SEPTEMBER, 30);
        } else {
            firstDay = LocalDate.of(currentYear, Month.OCTOBER, 1);
            lastDay = LocalDate.of(currentYear, Month.DECEMBER, 31);
        }
        return Date.from(lastDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取本年开始时间*/
    public static Date startYear(){
        LocalDate lastDay = today.withDayOfYear(1);
        return Date.from(lastDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /*获取本年结束时间*/
    public static Date endYear(){
        LocalDate lastYear = today.withDayOfYear(today.lengthOfYear());
        return Date.from(lastYear.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}
